package cn.jjzd.info.service;

import cn.jjzd.common.pojo.PageResult;
import cn.jjzd.common.utils.DateUtils;
import cn.jjzd.info.mapper.ConsultantMapper;
import cn.jjzd.info.mapper.ConsultantRelationMapper;
import cn.jjzd.info.pojo.Area;
import cn.jjzd.info.pojo.BusinessType;
import cn.jjzd.info.pojo.Consultant;
import cn.jjzd.info.pojo.ConsultantRelation;
import cn.jjzd.info.request.ConsultantRequest;
import cn.jjzd.info.response.ConsultantPageResponse;
import cn.jjzd.info.response.ConsultantResponse;
import cn.jjzd.info.response.SelectResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsultantService {
    @Autowired
    private ConsultantMapper consultantMapper;

    @Autowired
    private AreaService areaService;

    @Autowired
    private BusinessTypeService businessTypeService;

    @Autowired
    private ConsultantRelationMapper consultantRelationMapper;

    @Autowired
    private ConsultantRelationService consultantRelationService;

    /**
     * 根据区域ID和业务ID查询顾问信息
     * @param areaId
     * @param businessTypeId
     * @return
     */
    public List<ConsultantResponse> queryList(Long areaId, Long businessTypeId) {
        List<ConsultantPageResponse> consultantPageResponses = this.consultantMapper.queryConsultantList(null,null,null,areaId,businessTypeId);
        List<ConsultantResponse> consultantResponses = consultantPageResponses.stream().map(consultantPageResponse -> {
            ConsultantResponse consultantResponse = new ConsultantResponse();
            consultantResponse.setId(consultantPageResponse.getId());
            consultantResponse.setUrl(consultantPageResponse.getImagePath());
            consultantResponse.setName(consultantPageResponse.getName());
            consultantResponse.setTitle("（顾问：" + consultantPageResponse.getSerialNumber() + "）");
            consultantResponse.setBegood(consultantPageResponse.getGoodField());
            consultantResponse.setContent(consultantPageResponse.getDescription());
            return consultantResponse;
        }).collect(Collectors.toList());
        return consultantResponses;
    }

    public PageResult<ConsultantPageResponse> queryConsultantPage(String key, Integer page, Integer rows,Long areaId,Long businessTypeId) {
        //添加分页条件
        PageHelper.startPage(page,rows);

        List<ConsultantPageResponse> consultantPageResponses = consultantMapper.queryConsultantList(key,page,rows,areaId,businessTypeId);
        for (ConsultantPageResponse consultantPageResponse : consultantPageResponses) {
            consultantPageResponse.setCreateDateStr(DateUtils.dateToStr(consultantPageResponse.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
        }
        PageInfo pageInfo = new PageInfo<>(consultantPageResponses);

        //返回pageResult<>
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Integer queryMaxSerialNumber(){
        return consultantMapper.queryMaxSerialNumber();
    }

    @Transactional
    public void saveConsultant(ConsultantRequest consultant) {
        if(null != consultant){
            Consultant newConsultant = new Consultant();
            BeanUtils.copyProperties(consultant,newConsultant);
            newConsultant.setSerialNumber(queryMaxSerialNumber() == null ? 301 : queryMaxSerialNumber()+1);
            BusinessType businessType = businessTypeService.queryById(consultant.getBusinessTypeId());
            newConsultant.setGoodField("擅长领域:"+businessType.getBusinessName());
            newConsultant.setRecordStatus(true);
            newConsultant.setUpdateCount(0);
            newConsultant.setCreateDate(new Date());
            newConsultant.setUpdateDate(consultant.getCreateDate());
            newConsultant.setCreatorId(-1L);
            newConsultant.setUpdaterId(-1L);
            this.consultantMapper.insertSelective(newConsultant);
            if(null == consultant.getAreaId() || consultant.getAreaId().intValue() == 0){
                List<SelectResponse> responses = areaService.queryList();
                for (SelectResponse respons : responses) {
                    if(respons.getId().intValue() > 0){
                        ConsultantRelation consultantRelation = new ConsultantRelation();
                        consultantRelation.setConsultantId(newConsultant.getId());
                        consultantRelation.setAreaId(respons.getId());
                        consultantRelation.setBusinessTypeId(consultant.getBusinessTypeId());
                        consultantRelation.setRecordStatus(true);
                        consultantRelation.setUpdateCount(0);
                        consultantRelation.setCreateDate(new Date());
                        consultantRelation.setUpdateDate(consultant.getCreateDate());
                        consultantRelation.setCreatorId(-1L);
                        consultantRelation.setUpdaterId(-1L);
                        this.consultantRelationMapper.insertSelective(consultantRelation);
                    }
                }
            }else{
                ConsultantRelation consultantRelation = new ConsultantRelation();
                consultantRelation.setConsultantId(newConsultant.getId());
                consultantRelation.setAreaId(consultant.getAreaId());
                consultantRelation.setBusinessTypeId(consultant.getBusinessTypeId());
                consultantRelation.setRecordStatus(true);
                consultantRelation.setUpdateCount(0);
                consultantRelation.setCreateDate(new Date());
                consultantRelation.setUpdateDate(consultant.getCreateDate());
                consultantRelation.setCreatorId(-1L);
                consultantRelation.setUpdaterId(-1L);
                this.consultantRelationMapper.insertSelective(consultantRelation);
            }
        }

    }


    public void updateConsultant(ConsultantRequest consultant) {
        if(null != consultant && null != consultant.getId()){
            Consultant orginConsultant = consultantMapper.selectByPrimaryKey(consultant.getId());
            orginConsultant.setName(consultant.getName());
            orginConsultant.setDescription(consultant.getDescription());
            orginConsultant.setImagePath(consultant.getImagePath());
            orginConsultant.setUpdateCount(orginConsultant.getUpdateCount()+1);
            orginConsultant.setImagePath(consultant.getImagePath());
            this.consultantMapper.updateByPrimaryKey(orginConsultant);

            ConsultantRelation consultantRelation = this.consultantRelationService.queryConsultant(consultant.getId(),consultant.getOrginAreaId(),consultant.getOrginBusinessTypeId());

            consultantRelation.setAreaId(consultant.getAreaId());
            consultantRelation.setBusinessTypeId(consultant.getBusinessTypeId());
            this.consultantRelationMapper.updateByPrimaryKey(consultantRelation);
        }
    }

    @Transactional
    public void deleteById(ConsultantRequest consultantRequest) {
        Consultant consultant = consultantMapper.selectByPrimaryKey(consultantRequest.getId());
        if(null != consultant){
            consultant.setRecordStatus(false);
            this.consultantMapper.updateByPrimaryKey(consultant);

            ConsultantRelation consultantRelation = this.consultantRelationService.queryConsultant(consultantRequest.getId(),consultantRequest.getAreaId(),consultantRequest.getBusinessTypeId());
            if(null != consultantRelation){
                consultantRelation.setRecordStatus(false);
                this.consultantRelationMapper.updateByPrimaryKey(consultantRelation);
            }
        }
    }
}
